/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			alayid.cc
  \date			Sep 2012
  \author		TNick

  \brief		Contains the implementation of ALayID class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<usermsg.h>

#include	"alayid.h"

#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/pgspace.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */


/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
ALayID::ALayID	( CpgDoc * doc )
	: AtomicChg( doc )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ALayID::~ALayID	( void )
{
	if ( isDeleteVariant() )
	{
		delete new_layer_;
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					ALayID::doUndoRedo				( bool b_effect )
{
	PgLayGlob *		gl_lay;

	if ( b_effect )
	{ /* redo for create, undo for delete */
		if ( new_layer_->isGlobal() )
		{
			gl_lay = static_cast<PgLayGlob*>( new_layer_ );
			gl_lay->forceInsertInDoc( spc_comp_ );
		}
		else
		{
			space()->addLayer( new_layer_ );
		}
		PgLayer::raiseLayerCreated( assoc_space_, new_layer_ );
		return;
	}



	else if ( new_layer_->isGlobal() )
	{ /* undo for create, redo for delete */

		gl_lay = static_cast<PgLayGlob*>( new_layer_ );
		gl_lay->forceExtractFromDoc( spc_comp_ );
	}
	else
	{ /* undo for create, redo for delete */
		space()->remLayer( static_cast<PgLaySpc*>( new_layer_ ) );
	}
	PgLayer::raiseLayerDeleted( assoc_space_, new_layer_ );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ALayID *				ALayID::createLayer				(
		BBlocks::UserMsg & um, PgSpace * space, PgLayer * layer )
{
	ALayID *		ret = new ALayID( layer->document() );
	if ( layer->isGlobal() )
	{
		ret->setLongDescr( QObject::tr( "Created global layer %1" ).arg( layer->name() ) );
		/* we don't have to fill spc_comp_ because forceInsertInDoc() is ready to
		 * deal with empty lists
		 */
	}
	else if ( space == NULL )
	{
		Q_ASSERT( false );
		um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
					"Can't create a local layer without a space.\n"
					)
				);
		return NULL;
	}
	else
	{
		ret->setLongDescr( QObject::tr( "Created local layer %1 in space %2" )
						   .arg( layer->name() )
						   .arg( space->name() )
						   );
	}

	ret->assoc_doc_ = layer->document();
	ret->assoc_space_ = space;
	ret->new_layer_ = layer;


	ret->pushMe();
	return ret;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ALayID *				ALayID::deleteLayer				(
		BBlocks::UserMsg & um, PgSpace * space, PgLayer * layer )
{
	ALayID *		ret = new ALayID( layer->document() );

	ret->setReversed();
	if ( layer->isGlobal() )
	{
		ret->setLongDescr( QObject::tr( "Created global layer %1" ).arg( layer->name() ) );
	}
	else
	{
		if ( space == NULL )
		{
			PgLaySpc * ly_spc = static_cast<PgLaySpc*>( layer );
			space = ly_spc->space();
			if ( space == NULL )
			{
				Q_ASSERT( false );
				um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
							"Can't delete a local layer without a space.\n" )
						);
				delete ret;
				return NULL;
			}
		}
		ret->setLongDescr(
					QObject::tr( "Created local layer %1 in space %2" )
					.arg( layer->name() )
					);
	}

	ret->assoc_doc_ = layer->document();
	ret->assoc_space_ = space;
	ret->new_layer_ = layer;

	ret->pushMe();
	return ret;

}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
